/**
 * @file filter
 * @author liaoxuezhi@baidu.com
 */
/* eslint fecs-indent: [0, "space", 2, 2] */

import * as React from 'react';
import {
  PureComponent,
  PropTypes
} from 'react';
import {Renderer} from '../../renderer';
import {Mixin as FormsyMixin} from 'formsy-react';
import {decorate as Mixin} from 'react-mixin';
import * as cx from 'classnames';
import {
  FormGroup,
  HelpBlock,
  Checkbox,
  ControlLabel
} from 'react-bootstrap';
import * as assign from 'object-assign';
import {Enhance as FormItem} from '../form-item';

@FormItem({
  type: 'switch',
  validateSchema: 'http://amis.baidu.com/schemas/form.json#/definitions/formItem'
})
export default class FormCheckbox extends PureComponent {
  static propTypes = {
    className: PropTypes.string,
    formMode: PropTypes.string,
    formHorizontal: PropTypes.object
  };

  static defaultProps = {
    value: '',
    trueValue: true,
    falseValue: false
  };

  componentWillMount() {
    this.changeValue = this.changeValue.bind(this);
  }

  changeValue(event) {
    const target = event.currentTarget;
    const {
      trueValue,
      falseValue,
      setValue
    } = this.props;

    setValue(target.checked ? trueValue : falseValue);
  }

  render() {
    const {
      formMode,
      inputClassName,
      getValue,
      trueValue
    } = this.props;
    let value = getValue();

    return (
      <div className={cx("checkbox", inputClassName, formMode === 'inline' ? 'm-l m-r-xs' : '')}>
        <Checkbox
          className="i-switch"
          checked={value == trueValue}
          onChange={this.changeValue}
          disabled={this.props.isFormDisabled() || this.props.disabled}
          inline
        ><i /></Checkbox>
      </div>
    );
  }
}
